/*
  This file is a part of hashutils package.
  (c) 2004 Zeljko Vrba <zvrba@globalnet.hr>

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stddef.h>
#include "cdt_interval.h"

static char rcsid[] = "$Id: cdt_interval.c 2 2007-03-29 14:43:55Z zeljko.vrba $";

static Vmalloc_t *IntervalSet_vmpool = NULL;

/**
   Intervals X=[lb1, ub1) and Y=[lb2, ub2) are totally ordered by the following
   rules:
   1. X < Y  <=> ub1 < lb2
   2. X > Y  <=> lb1 > ub2
   3. otherwise they are considered 'equal' and can be merged in a single
      interval.
*/
static int interval_comparf(Dt_t *dt, Void_t *key1, Void_t *key2, Dtdisc_t *disc)
{
	unsigned long *i1 = key1, *i2 = key2;

	if(i1[1] < i2[0]) return -1;
	if(i1[0] > i2[1]) return 1;
	return 0;
}

static Void_t *interval_makef(Dt_t *dt, Void_t *obj, Dtdisc_t *disc)
{
	return vmalloc(IntervalSet_vmpool, sizeof(struct interval));
}

static void interval_freef(Dt_t *dt, Void_t *obj, Dtdisc_t *disc)
{
	vmfree(IntervalSet_vmpool, obj);
}

Dtdisc_t Cdt_IntervalDiscipline = {
	offsetof(struct interval, bounds), /* key offset */
	2*sizeof(unsigned long),	/* key size */
	0,							/* link: beginning of struct */
	interval_makef,			    /* copy method */
	interval_freef,				/* free method */
	interval_comparf,			/* compare */
	NULL,						/* hash: use internal function */
	NULL,
	NULL
};

Dt_t *IntervalSet_create(void)
{
	if(!IntervalSet_vmpool &&
	   !(IntervalSet_vmpool = vmopen(Vmdcheap, Vmpool, VM_TRUST))) return NULL;
	return dtopen(&Cdt_IntervalDiscipline, Dtobag);
}

/**
   Insert a single number in the set of intervals. This just inserts the
   interval [n, n+1).

   @param	n	Number to insert.

   @return Pointer to interval in which the number was inserted.
*/
struct interval *IntervalSet_insert1(Dt_t *dt, unsigned long n)
{
	return IntervalSet_insert2(dt, n, n+1);
}

/**
   Merge two intervals.

   @return Merged interval. On error, an empty interval [0,0) is returned.
*/
static struct interval interval_merge(
	const struct interval *i1,
	const struct interval *i2)
{
	struct interval i;

	/* swap: i1 lower bound should be <= to i2 lower bound. */
	if(i1->bounds[0] > i2->bounds[0]) {
		const struct interval *tmp = i1; i1 = i2; i2 = tmp;
	}

	/* the upper bound of resulting interval is greater of both upper bounds */
	if(i2->bounds[0] <= i1->bounds[1]) {
		i.bounds[0] = i1->bounds[0];
		i.bounds[1] = i1->bounds[1] > i2->bounds[1] ? 
			i1->bounds[1] : i2->bounds[1];
	} else {
		/* intervals don't even touch; return empty interval */
		i.bounds[0] = i.bounds[1] = 0;
	}
	return i;
}

/*
  Insert an interval [lb, ub) in the set of intervals.
  @param	lb,ub	Lower and upper bound of interval.
  
  @return	Pointer to resulting interval or NULL on failure.

  @note	First all intervals 'equal' to the new interval are sought for,
  deleted and merged with the new interval. Then a single new interval is
  inserted.
*/
struct interval *IntervalSet_insert2(
	Dt_t *dt, unsigned long lb, unsigned long ub)
{
	struct interval result, *found;

	result.bounds[0] = lb; result.bounds[1] = ub;

	while((found = dtmatch(dt, result.bounds))) {
		result = interval_merge(&result, found);
		if(!result.bounds[0] && !result.bounds[1]) return NULL;
		dtdelete(dt, found);
	}
	return dtinsert(dt, &result); /* discipline copies the struct */
}
